package com.anjbo.controller.circle.estatedeal;

import com.anjbo.bean.estatedeal.*;
import com.anjbo.common.*;
import com.anjbo.controller.common.StatisticsController;
import com.anjbo.service.circle.estatedeal.DealDataService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.time.DateUtils;
import org.jsoup.helper.DataUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;


@Controller
@RequestMapping("app/circle/estatedeal/v")
public class DealDataController {

    @Resource
    private DealDataService dealDataService;

    private Map<String, String> mediators = new HashMap<String, String>();

    {
        try {
            Properties prop = new Properties();
            prop.load(new InputStreamReader(getClass().getResourceAsStream("/mediators.properties"), "UTF-8"));
            mediators = new HashMap<String, String>((Map) prop);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("data")
    @ResponseBody
    public Object data(@RequestBody Map<String, String> params, HttpServletRequest request) {
        String city = MapUtils.getString(params, "city");
        new StatisticsController().statistics(request, "2000-8");//统计数据浏览量
        if (city.equals("深圳")) {
            return szData();
        } else if (city.equals("东莞")) {
            return dgData();
        } else if (city.equals("广州")) {
            return gzData();
        } else if (city.equals("惠州")) {
            return hzData();
        } else if (city.equals("厦门")) {
            return xmData();
        } else {
            RespDataObject<Object> resp = new RespDataObject<Object>();
            RespHelper.setFailDataObject(resp, null, RespStatusEnum.FAIL.getMsg());
            return resp;
        }
    }

    /**
     * 深圳数据接口
     *
     * @return
     */
    @RequestMapping("sz/data")
    @ResponseBody
    public Object szData() {
        RespDataObject<SZDealRespDto> resp = new RespDataObject<SZDealRespDto>();
        SZDealRespDto respDto = new SZDealRespDto();
        try {
            List<Map<String, SZDealDataDto>> scndMonth = new ArrayList<Map<String, SZDealDataDto>>();
            List<Map<String, SZDealDataDto>> oneMonth = new ArrayList<Map<String, SZDealDataDto>>();
            List<Map<String, SZDealDataDto>> scndDay = new ArrayList<Map<String, SZDealDataDto>>();
            List<Map<String, SZDealDataDto>> oneDay = new ArrayList<Map<String, SZDealDataDto>>();


            List<SZDealDto> szScndMonthData = dealDataService.selectSZMonthScndData();
            List<SZDealDto> szScndMonthDetail = dealDataService.selectSZMonthScndDetail();
            for (SZDealDto dto : szScndMonthData) {
                SZDealDataDto szScndDealDataDto = new SZDealDataDto();
                szScndDealDataDto.setTotalNum(dto.getTotalNum());
                szScndDealDataDto.setMediumDetail(getSZDetailByMonth(szScndMonthDetail, dto.getMonth(), false));
                Map<String, SZDealDataDto> scndMap = new HashMap<String, SZDealDataDto>();
                scndMap.put(formatMonth(dto.getMonth()), szScndDealDataDto);
                if (szScndDealDataDto.getTotalNum() != 0 && scndMonth.size() < 12) {
                    scndMonth.add(scndMap);
                }
            }

            List<SZDealDto> szScndDayData = dealDataService.selectSZDayScndData();
            List<SZDealDto> szScndDayDetail = dealDataService.selectSZDayScndDetail();
            for (SZDealDto dto : szScndDayData) {
                SZDealDataDto szScndDealDataDto = new SZDealDataDto();
                szScndDealDataDto.setTotalNum(dto.getTotalNum());
                szScndDealDataDto.setMediumDetail(getSZDetailByDay(szScndDayDetail, dto.getDate(), false));
                Map<String, SZDealDataDto> scndMap = new HashMap<String, SZDealDataDto>();
                scndMap.put(formatDay(dto.getDate()), szScndDealDataDto);
                if (szScndDealDataDto.getTotalNum() != 0 && scndDay.size() < 29) {
                    scndDay.add(scndMap);
                }
            }


            List<SZDealDto> szOneMonthData = dealDataService.selectSZMonthOneData();
            List<SZDealDto> szOneMonthDetail = dealDataService.selectSZMonthOneDetail();
            for (SZDealDto dto : szOneMonthData) {
                SZDealDataDto szOneDealDataDto = new SZDealDataDto();
                szOneDealDataDto.setTotalNum(dto.getTotalNum());
                szOneDealDataDto.setOneDetail(getSZDetailByMonth(szOneMonthDetail, dto.getMonth(), true));
                szOneDealDataDto.setOneAvgDetail(getSZDetailByMonthPrice(szOneMonthDetail, dto.getMonth()));//??
                Map<String, SZDealDataDto> oneMap = new HashMap<String, SZDealDataDto>();
                oneMap.put(formatMonth(dto.getMonth()), szOneDealDataDto);
                if (szOneDealDataDto.getTotalNum() != 0 && oneMonth.size() < 12) {
                    oneMonth.add(oneMap);
                }
            }


            List<SZDealDto> szOneDayData = dealDataService.selectSZDayOneData();
            List<SZDealDto> szOneDayDetail = dealDataService.selectSZDayOneDetail();
            for (SZDealDto dto : szOneDayData) {
                SZDealDataDto szOneDealDataDto = new SZDealDataDto();
                szOneDealDataDto.setTotalNum(dto.getTotalNum());
                szOneDealDataDto.setOneDetail(getSZDetailByDay(szOneDayDetail, dto.getDate(), true));
                szOneDealDataDto.setOneAvgDetail(getSZDetailByDayPrice(szOneDayDetail, dto.getDate()));//??
                Map<String, SZDealDataDto> oneMap = new HashMap<String, SZDealDataDto>();
                oneMap.put(formatDay(dto.getDate()), szOneDealDataDto);
                if (szOneDealDataDto.getTotalNum() != 0 && oneDay.size() < 29) {
                    oneDay.add(oneMap);
                }
            }

            reverseList(oneDay, scndDay, oneMonth, scndMonth);
            respDto.setOneDay(oneDay);
            respDto.setScndDay(scndDay);
            respDto.setOneMonth(oneMonth);
            respDto.setScndMonth(scndMonth);
            RespHelper.setSuccessDataObject(resp, respDto);
        } catch (Exception e) {
            RespHelper.setFailDataObject(resp, null, RespStatusEnum.FAIL.getMsg());
        }
        return resp;
    }


    /**
     * 东莞数据接口
     *
     * @return
     */
    @RequestMapping("dg/data")
    @ResponseBody
    public Object dgData() {
        RespDataObject<DGDealRespDto> resp = new RespDataObject<DGDealRespDto>();
        DGDealRespDto respDto = new DGDealRespDto();
        try {
            List<Map<String, DGDealDataDto>> scndMonth = new ArrayList<Map<String, DGDealDataDto>>();
            List<Map<String, DGDealDataDto>> oneMonth = new ArrayList<Map<String, DGDealDataDto>>();
            List<Map<String, DGDealDataDto>> scndDay = new ArrayList<Map<String, DGDealDataDto>>();
            List<Map<String, DGDealDataDto>> oneDay = new ArrayList<Map<String, DGDealDataDto>>();

            //按月统计数据
            List<DGDealDto> dgMonthData = dealDataService.selectDGMonthData();
            //按月统计详情
            List<DGDealDto> dgMonthDetail = dealDataService.selectDGMonthDetail();

            for (DGDealDto dto : dgMonthData) {
                if (dto.getType() == 2) {
                    DGDealDataDto dgScndDealDataDto = new DGDealDataDto();
                    dgScndDealDataDto.setTotalNum(dto.getTotalNum());
                    dgScndDealDataDto.setDetail(getDGDetailByMonthAndType(dgMonthDetail, dto.getMonth(), dto.getType()));
                    Map<String, DGDealDataDto> scndMap = new HashMap<String, DGDealDataDto>();
                    scndMap.put(formatMonth(dto.getMonth()), dgScndDealDataDto);
                    if (dgScndDealDataDto.getTotalNum() != 0 && scndMonth.size() < 12) {
                        scndMonth.add(scndMap);
                    }
                } else if (dto.getType() == 1) {
                    DGDealDataDto dgOneDealDataDto = new DGDealDataDto();
                    dgOneDealDataDto.setTotalNum(dto.getTotalNum());
                    dgOneDealDataDto.setDetail(getDGDetailByMonthAndType(dgMonthDetail, dto.getMonth(), dto.getType()));
                    Map<String, DGDealDataDto> oneMap = new HashMap<String, DGDealDataDto>();
                    oneMap.put(formatMonth(dto.getMonth()), dgOneDealDataDto);
                    if (dgOneDealDataDto.getTotalNum() != 0 && oneMonth.size() < 12) {
                        oneMonth.add(oneMap);
                    }
                }
            }

            //按天统计数据
            List<DGDealDto> dgDayData = dealDataService.selectDGDayData();
            //按天统计详情
            List<DGDealDto> dgDayDetail = dealDataService.selectDGDayDetail();

            for (DGDealDto dto : dgDayData) {
                if (dto.getType() == 2) {
                    DGDealDataDto dgScndDealDataDto = new DGDealDataDto();
                    dgScndDealDataDto.setTotalNum(dto.getTotalNum());
                    dgScndDealDataDto.setDetail(getDGDetailByDayAndType(dgDayDetail, dto.getDate(), dto.getType()));
                    Map<String, DGDealDataDto> scndMap = new HashMap<String, DGDealDataDto>();
                    scndMap.put(formatDay(dto.getDate()), dgScndDealDataDto);
                    if (dgScndDealDataDto.getTotalNum() != 0 && scndDay.size() < 29) {
                        scndDay.add(scndMap);
                    }
                } else if (dto.getType() == 1) {
                    DGDealDataDto dgOneDealDataDto = new DGDealDataDto();
                    dgOneDealDataDto.setTotalNum(dto.getTotalNum());
                    dgOneDealDataDto.setDetail(getDGDetailByDayAndType(dgDayDetail, dto.getDate(), dto.getType()));
                    Map<String, DGDealDataDto> oneMap = new HashMap<String, DGDealDataDto>();
                    oneMap.put(formatDay(dto.getDate()), dgOneDealDataDto);
                    if (dgOneDealDataDto.getTotalNum() != 0 && oneDay.size() < 29) {
                        oneDay.add(oneMap);
                    }
                }
            }
            reverseList(oneDay, scndDay, oneMonth, scndMonth);
            respDto.setOneDay(oneDay);
            respDto.setScndDay(scndDay);
            respDto.setOneMonth(oneMonth);
            respDto.setScndMonth(scndMonth);
            RespHelper.setSuccessDataObject(resp, respDto);
        } catch (Exception e) {
            RespHelper.setFailDataObject(resp, null, RespStatusEnum.FAIL.getMsg());
        }
        return resp;
    }

    /**
     * 广州数据接口
     *
     * @return
     */
    @RequestMapping("gz/data")
    @ResponseBody
    public Object gzData() {

        RespDataObject<GZDealRespDto> resp = new RespDataObject<GZDealRespDto>();
        GZDealRespDto respDto = new GZDealRespDto();
        try {
            List<Map<String, GZDealDataDto>> scndMonth = new ArrayList<Map<String, GZDealDataDto>>();
            List<Map<String, GZDealDataDto>> oneMonth = new ArrayList<Map<String, GZDealDataDto>>();
            List<Map<String, GZDealDataDto>> scndDay = new ArrayList<Map<String, GZDealDataDto>>();
            List<Map<String, GZDealDataDto>> oneDay = new ArrayList<Map<String, GZDealDataDto>>();

            List<GZDealDto> gzMonthData =  dealDataService.selectGZMonthData();
            List<GZDealDto> gzMonthDetail = dealDataService.selectGZMonthDetail();


            for (GZDealDto dto : gzMonthData) {
                if (dto.getType() == 2) {
                    GZDealDataDto gzScndDealDataDto = new GZDealDataDto();
                    gzScndDealDataDto.setTotalNum(dto.getTotalNum());
                    Map<String, GZDealDataDto> scndMap = new HashMap<String, GZDealDataDto>();
                    scndMap.put(formatMonth(dto.getMonth()), gzScndDealDataDto);
                    if (gzScndDealDataDto.getTotalNum() != 0 && scndMonth.size() < 12) {
                        scndMonth.add(scndMap);
                    }
                } else if (dto.getType() == 1) {
                    GZDealDataDto gzOneDealDataDto = new GZDealDataDto();
                    gzOneDealDataDto.setTotalNum(dto.getTotalNum());
                    gzOneDealDataDto.setDetail(getGZDetailByMonthAndType(gzMonthDetail, dto.getMonth(), dto.getType()));
                    Map<String, GZDealDataDto> oneMap = new HashMap<String, GZDealDataDto>();
                    oneMap.put(formatMonth(dto.getMonth()), gzOneDealDataDto);
                    if (gzOneDealDataDto.getTotalNum() != 0 && oneMonth.size() < 12) {
                        oneMonth.add(oneMap);
                    }
                }
            }

            List<GZDealDto> gzDayData = dealDataService.selectGZDayData();
            List<GZDealDto> gzDayDetail = dealDataService.selectGZDayDetail();

            for (GZDealDto dto : gzDayData) {
                if (dto.getType() == 2) {
                    GZDealDataDto gzScndDealDataDto = new GZDealDataDto();
                    gzScndDealDataDto.setTotalNum(dto.getTotalNum());
                    Map<String, GZDealDataDto> scndMap = new HashMap<String, GZDealDataDto>();
                    scndMap.put(formatDay(dto.getDate()), gzScndDealDataDto);
                    if (gzScndDealDataDto.getTotalNum() != 0 && scndDay.size() < 29) {
                        scndDay.add(scndMap);
                    }
                } else if (dto.getType() == 1) {
                    GZDealDataDto gzOneDealDataDto = new GZDealDataDto();
                    gzOneDealDataDto.setTotalNum(dto.getTotalNum());
                    gzOneDealDataDto.setDetail(getGZDetailByDayAndType(gzDayDetail, dto.getDate(), dto.getType()));
                    Map<String, GZDealDataDto> oneMap = new HashMap<String, GZDealDataDto>();
                    oneMap.put(formatDay(dto.getDate()), gzOneDealDataDto);
                    if (gzOneDealDataDto.getTotalNum() != 0 && oneDay.size() < 29) {
                        oneDay.add(oneMap);
                    }
                }
            }
            reverseList(oneDay, scndDay, oneMonth, scndMonth);
            respDto.setOneDay(oneDay);
            respDto.setScndDay(scndDay);
            respDto.setOneMonth(oneMonth);
            respDto.setScndMonth(scndMonth);
            RespHelper.setSuccessDataObject(resp, respDto);
        } catch (Exception e) {
            RespHelper.setFailDataObject(resp, null, RespStatusEnum.FAIL.getMsg());
        }
        return resp;
    }

    /**
     * 惠州数据接口
     *
     * @return
     */
    @RequestMapping("hz/data")
    @ResponseBody
    public Object hzData() {
        RespDataObject<HZDealRespDto> resp = new RespDataObject<HZDealRespDto>();
        HZDealRespDto respDto = new HZDealRespDto();
        try {
            List<Map<String, HZDealDataDto>> scndMonth = new ArrayList<Map<String, HZDealDataDto>>();
            List<Map<String, HZDealDataDto>> oneMonth = new ArrayList<Map<String, HZDealDataDto>>();
            List<Map<String, HZDealDataDto>> scndDay = new ArrayList<Map<String, HZDealDataDto>>();
            List<Map<String, HZDealDataDto>> oneDay = new ArrayList<Map<String, HZDealDataDto>>();


            List<HZDealDto> hzMonthData = dealDataService.selectHZMonthData();
            List<HZDealDto> hzDayData = dealDataService.selectHZDayData();

            for (HZDealDto dto : hzMonthData) {
                if (dto.getType() == 2) {
                    HZDealDataDto hzScndDealDataDto = new HZDealDataDto();
                    hzScndDealDataDto.setTotalNum(dto.getTotalNum());
                    Map<String, HZDealDataDto> scndMap = new HashMap<String, HZDealDataDto>();
                    scndMap.put(formatMonth(dto.getMonth()), hzScndDealDataDto);
                    if (hzScndDealDataDto.getTotalNum() != 0 && scndMonth.size() < 12) {
                        scndMonth.add(scndMap);
                    }
                } else if (dto.getType() == 1) {
                    HZDealDataDto hzOneDealDataDto = new HZDealDataDto();
                    hzOneDealDataDto.setTotalNum(dto.getTotalNum());
                    Map<String, HZDealDataDto> oneMap = new HashMap<String, HZDealDataDto>();
                    oneMap.put(formatMonth(dto.getMonth()), hzOneDealDataDto);
                    if (hzOneDealDataDto.getTotalNum() != 0 && oneMonth.size() < 12) {
                        oneMonth.add(oneMap);
                    }
                }
            }

            for (HZDealDto dto : hzDayData) {
                if (dto.getType() == 2) {
                    HZDealDataDto hzScndDealDataDto = new HZDealDataDto();
                    hzScndDealDataDto.setTotalNum(dto.getTotalNum());
                    Map<String, HZDealDataDto> scndMap = new HashMap<String, HZDealDataDto>();
                    scndMap.put(formatDay(dto.getDate()), hzScndDealDataDto);
                    if (hzScndDealDataDto.getTotalNum() != 0 && scndDay.size() < 29) {
                        scndDay.add(scndMap);
                    }
                } else if (dto.getType() == 1) {
                    HZDealDataDto hzOneDealDataDto = new HZDealDataDto();
                    hzOneDealDataDto.setTotalNum(dto.getTotalNum());
                    Map<String, HZDealDataDto> oneMap = new HashMap<String, HZDealDataDto>();
                    oneMap.put(formatDay(dto.getDate()), hzOneDealDataDto);
                    if (hzOneDealDataDto.getTotalNum() != 0 && oneDay.size() < 29) {
                        oneDay.add(oneMap);
                    }
                }
            }
            reverseList(oneDay, scndDay, oneMonth, scndMonth);
            respDto.setOneDay(oneDay);
            respDto.setScndDay(scndDay);
            respDto.setOneMonth(oneMonth);
            respDto.setScndMonth(scndMonth);
            RespHelper.setSuccessDataObject(resp, respDto);
        } catch (Exception e) {
            RespHelper.setFailDataObject(resp, null, RespStatusEnum.FAIL.getMsg());
        }
        return resp;
    }


    /**
     * 厦门数据接口
     *
     * @return
     */
    @RequestMapping("xm/data")
    @ResponseBody
    public Object xmData() {
        RespDataObject<XMDealRespDto> resp = new RespDataObject<XMDealRespDto>();
        try {
            XMDealRespDto respDto = new XMDealRespDto();
            List<Map<String, XMDealDataDto>> scndMonth = new ArrayList<Map<String, XMDealDataDto>>();
            List<Map<String, XMDealDataDto>> oneMonth = new ArrayList<Map<String, XMDealDataDto>>();
            List<Map<String, XMDealDataDto>> scndDay = new ArrayList<Map<String, XMDealDataDto>>();
            List<Map<String, XMDealDataDto>> oneDay = new ArrayList<Map<String, XMDealDataDto>>();

            List<XMDealDto> xmMonthData = dealDataService.selectXMMonthData();
            List<XMDealDto> xmDayData = dealDataService.selectXMDayData();

            for (XMDealDto dto : xmMonthData) {
                if (dto.getType() == 2) {
                    XMDealDataDto xmScndDealDataDto = new XMDealDataDto();
                    xmScndDealDataDto.setTotalNum(dto.getTotalNum());
                    Map<String, XMDealDataDto> scndMap = new HashMap<String, XMDealDataDto>();
                    scndMap.put(formatMonth(dto.getMonth()), xmScndDealDataDto);
                    if (xmScndDealDataDto.getTotalNum() != 0 && scndMonth.size() < 12) {
                        scndMonth.add(scndMap);
                    }
                } else if (dto.getType() == 1) {
                    XMDealDataDto xmOneDealDataDto = new XMDealDataDto();
                    xmOneDealDataDto.setTotalNum(dto.getTotalNum());
                    Map<String, XMDealDataDto> oneMap = new HashMap<String, XMDealDataDto>();
                    oneMap.put(formatMonth(dto.getMonth()), xmOneDealDataDto);
                    if (xmOneDealDataDto.getTotalNum() != 0 && oneMonth.size() < 12) {
                        oneMonth.add(oneMap);
                    }
                }
            }

            for (XMDealDto dto : xmDayData) {
                if (dto.getType() == 2) {
                    XMDealDataDto xmScndDealDataDto = new XMDealDataDto();
                    xmScndDealDataDto.setTotalNum(dto.getTotalNum());
                    Map<String, XMDealDataDto> scndMap = new HashMap<String, XMDealDataDto>();
                    scndMap.put(formatDay(dto.getDate()), xmScndDealDataDto);
                    if (xmScndDealDataDto.getTotalNum() != 0 && scndDay.size() < 29) {
                        scndDay.add(scndMap);
                    }
                } else if (dto.getType() == 1) {
                    XMDealDataDto xmOneDealDataDto = new XMDealDataDto();
                    xmOneDealDataDto.setTotalNum(dto.getTotalNum());
                    Map<String, XMDealDataDto> oneMap = new HashMap<String, XMDealDataDto>();
                    oneMap.put(formatDay(dto.getDate()), xmOneDealDataDto);
                    if (xmOneDealDataDto.getTotalNum() != 0 && oneDay.size() < 29) {
                        oneDay.add(oneMap);
                    }
                }
            }
            reverseList(oneDay, scndDay, oneMonth, scndMonth);
            respDto.setOneDay(oneDay);
            respDto.setScndDay(scndDay);
            respDto.setOneMonth(oneMonth);
            respDto.setScndMonth(scndMonth);
            RespHelper.setSuccessDataObject(resp, respDto);
        } catch (Exception e) {
            RespHelper.setFailDataObject(resp, null, RespStatusEnum.FAIL.getMsg());
        }
        return resp;
    }


    private static void reverseList(List... list) {
        for (List lt : list) {
            Collections.reverse(lt);
        }
    }

    private static List<String> getMonthsBefore12() {
        List<String> months = new ArrayList<String>();
        Date now = new Date();
        for (int i = 0; i < 12; i++) {
            months.add(DateUtil.getDateByFmt(DateUtils.addMonths(now, -i), "yyyy-MM"));
        }
        return months;
    }

    private static List<String> getDaysBefore30() {
        List<String> days = new ArrayList<String>();
        Date now = new Date();
        for (int i = 0; i < 30; i++) {
            days.add(DateUtil.getDateByFmt(DateUtils.addDays(now, -i), "yyyy-MM-dd"));
        }
        return days;
    }

    private static String formatMonth(String month) {
        if (StringUtils.isNotEmpty(month)) {
            String s = month.replace("-", "/");
            String r = s.substring(2);
            return r;
        }
        return null;
    }

    private static String formatDay(String day) {
        if (StringUtils.isNotEmpty(day)) {
            String s = day.replace("-", "/");
            String r = s.substring(5);
            return r;
        }
        return null;
    }

    private static String formatDay(Date date) {
        String dateByFmt = DateUtil.getDateByFmt(date, "yyyy-MM-dd");
        return formatDay(dateByFmt);
    }

    private void formatMediatorName(List<SZDealDto> list) {
        if (list != null) {
            for (SZDealDto dto : list) {
                String oldName = dto.getMediumName();
                String newName = mediators.get(oldName);
                if (StringUtils.isNotEmpty(newName)) {
                    dto.setMediumName(newName);
                } else {
                    dto.setMediumName(clipName(oldName));
                }
            }
        }
    }

    /*获取缩略中介名称*/
    private String clipName(String name) {
        if (StringUtils.isNotEmpty(name)) {
            return name.replace("深圳市", "").replace("(深圳)", "").replace("深圳", "").replace("有限公司", "");
        }
        return null;
    }

    private List<SZDealDto> getSZDetailByMonth(List<SZDealDto> source, String month, Boolean sort) {
        formatMediatorName(source);
        ArrayList<SZDealDto> r = new ArrayList<SZDealDto>();
        for (SZDealDto dto : source) {
            if (dto.getMonth().equals(month)) {
                if (r.size() < 10) {
                    r.add(dto);
                }
            }
        }
        if (sort) {
            Collections.reverse(r);
        }
        return r;
    }


    private List<SZDealDto> getSZDetailByMonthPrice(List<SZDealDto> source, String month) {
        formatMediatorName(source);
        ArrayList<SZDealDto> r = new ArrayList<SZDealDto>();
        for (SZDealDto dto : source) {
            if (dto.getMonth().equals(month)) {
                if (r.size() < 10) {
                    r.add(dto);
                }
            }
        }
        Collections.sort(r, new Comparator<SZDealDto>() {
            @Override
            public int compare(SZDealDto o1, SZDealDto o2) {
                return o2.getAvgPrice().intValue() - o1.getAvgPrice().intValue();
            }
        });
        return r;
    }

    private List<SZDealDto> getSZDetailByDay(List<SZDealDto> source, Date date, Boolean sort) {
        formatMediatorName(source);
        ArrayList<SZDealDto> r = new ArrayList<SZDealDto>();
        for (SZDealDto dto : source) {
            if (dto.getDate().equals(date)) {
                if (r.size() < 10) {
                    r.add(dto);
                }
            }
        }
        if (sort) {
            Collections.reverse(r);
        }
        return r;
    }

    private List<SZDealDto> getSZDetailByDayPrice(List<SZDealDto> source, Date date) {
        formatMediatorName(source);
        ArrayList<SZDealDto> r = new ArrayList<SZDealDto>();
        for (SZDealDto dto : source) {
            if (dto.getDate().equals(date)) {
                if (r.size() < 10) {
                    r.add(dto);
                }
            }
        }
        Collections.sort(r, new Comparator<SZDealDto>() {
            @Override
            public int compare(SZDealDto o1, SZDealDto o2) {
                return o2.getAvgPrice().intValue() - o1.getAvgPrice().intValue();
            }
        });
        return r;
    }

    private List<GZDealDto> getGZDetailByMonthAndType(List<GZDealDto> source, String month, Integer type) {
        ArrayList<GZDealDto> r = new ArrayList<GZDealDto>();
        for (GZDealDto dto : source) {
            if (dto.getType() == type && dto.getMonth().equals(month)) {
                if (r.size() < 10) {
                    r.add(dto);
                }
            }
        }
        return r;
    }

    private List<GZDealDto> getGZDetailByDayAndType(List<GZDealDto> source, Date date, Integer type) {
        ArrayList<GZDealDto> r = new ArrayList<GZDealDto>();
        for (GZDealDto dto : source) {
            if (dto.getType() == type && dto.getDate().equals(date)) {
                if (r.size() < 10) {
                    r.add(dto);
                }
            }
        }
        return r;
    }

    private List<DGDealDto> getDGDetailByMonthAndType(List<DGDealDto> source, String month, Integer type) {
        ArrayList<DGDealDto> r = new ArrayList<DGDealDto>();
        for (DGDealDto dto : source) {
            if (dto.getType() == type && dto.getMonth().equals(month)) {
                if (r.size() < 10) {
                    r.add(dto);
                }
            }
        }
        Collections.sort(r, new Comparator<DGDealDto>() {
            @Override
            public int compare(DGDealDto o1, DGDealDto o2) {
                return o2.getTotalNum()-o1.getTotalNum();
            }
        });
        return r;
    }

    private List<DGDealDto> getDGDetailByDayAndType(List<DGDealDto> source, Date date, Integer type) {
        ArrayList<DGDealDto> r = new ArrayList<DGDealDto>();
        for (DGDealDto dto : source) {
            if (dto.getType() == type && dto.getDate().equals(date)) {
                if (r.size() < 10) {
                    r.add(dto);
                }
            }
        }
        Collections.sort(r, new Comparator<DGDealDto>() {
            @Override
            public int compare(DGDealDto o1, DGDealDto o2) {
                return o2.getTotalNum()-o1.getTotalNum();
            }
        });
        return r;
    }


    public static void main(String[] args) {
        String s = formatDay("2017-03-08");
        System.out.println(s);
    }
}
